<template>
  <div id="chart" class="sale-order-chart"></div>
</template>

<script>
import * as echarts from 'echarts'
import { getAction } from '@api/manage'

export default {
  name: 'HfSaleOrderChart',
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['sourceId'] = val
          this.loadData()
        }
      }
    }
  },
  data() {
    return {
      queryParam: {},
      option: {
        legend: {},
        tooltip: {},
        dataset: {
          source: [
            ['product', '数量', '已出'],
            ['Matcha Latte', 43.3, 85.8, 93.7],
            ['Milk Tea', 83.1, 73.4, 55.1],
            ['Cheese Cocoa', 86.4, 65.2, 82.5],
            ['Walnut Brownie', 72.4, 53.9, 39.1]
          ]
        },
        xAxis: { type: 'category' },
        yAxis: {},
        // Declare several bar series, each will be mapped
        // to a column of dataset.source by default.
        series: [{ type: 'bar' }, { type: 'bar' }, { type: 'bar' }]
      },
      url: {
        list: '/sale/hfSaleOrder/listHfSaleorderDetailByMainId'
      },
      dataSource: []
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
    },
    loadData() {
      getAction(this.url.list, this.queryParam).then(res => {
        if (res.success) {
          this.dataSource = res.result.records || res.result
          if (this.dataSource.length > 0) {
            this.handleData(this.dataSource)
          }
        }
      })
    },
    handleData(data) {
      let dataSource = []
      data.forEach(item => {
        dataSource.push(item.deliveryDateList)
      })
      console.log('dataSource', dataSource)
    }
  },
  mounted() {
    this.$nextTick(() => {
      const chart2 = echarts.init(document.getElementById('chart'))
      chart2.setOption(this.option)
    })
  }
}
</script>

<style scoped>
.sale-order-chart {
  width: 100%;
  height: 300px;
}
</style>
